from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import os
import csv
import json
import numpy as np
tsne = TSNE(n_components=2, random_state=0)

ids={}
hts={}
lhts={}
prev=None
with open('D:\\Work\\kusuri\\first100ids.tsv','r',newline='\n',encoding='utf-8')as fin:
    reader = csv.reader(fin, delimiter='\t')
    for line in reader:
        if line[0] not in hts.keys():
            if prev is not None:
                lhts[prev[0]]=prev[1:]
            hts[line[0]]=line[1:]
            for item in line[2:]:
                ids[item]={'ht':'', 'dt':'','ftr':None,'sim':None}
                ids[item]['ht']=line[0]
                ids[item]['dt'] = line[1]
        prev=line
# collist = ['wip', 'haventseen', 'ootd', 'bekind', 'personalfinance', 'cozyathome', 'RoomTour', 'theatrekids',
#            'ImAGhost', 'holidaytiktok', 'halloweenlook', 'happyhalloween', 'welldone', 'motivationmonday',
#            'thinkingabout', 'nonuancenovember', 'ourtype', 'fanedit', 'needtoknow', 'cleantok', 'graphicdesign',
#            'readysetshop', 'holidaysourway', 'onlinedating', 'myhobby', 'tiktokfood', 'whereilive',
#            'myrecommendation', 'worldseries', 'animation', 'cocinando', 'easydiy', 'diceroll', 'rnbvibes',
#            'festivefashion', 'holidaydecor', 'nbadraft', 'halloweenishere', 'christmas2020', 'howbizarre',
#            'sfxmakeup', 'givingthanks', 'holidayvibes', 'gamenight', 'happyholidays', 'carsoftiktok',
#            'fallguysmoments', 'interiordesign', 'homecooked', 'veteransday', 'youwantmore', 'coldweather',
#            'wildanimals', 'mycostume', 'meleaving', 'mypfp', 'catchphrases', 'watchmegrow', 'holidaycrafts',
#            'growupwithme', 'clingypet', 'happyhanukkah', 'lunarnewyear', 'tabletop', 'comfortfood',
#            'selfimprovement', '2021affirmations', 'perfectmatch', 'givingszn', 'holidaycountdown', 'bakingszn',
#            'holidaymusic', 'familyimpression', 'inkdrawing', 'WeekendVibes', 'recordsday', 'productivity',
#            'smallbusiness', 'falldiy', 'whenwewereyounger', 'yellow', 'ComingOfAge', 'artmas', 'gaminglife',
#            'gamingsetup', 'hellowinter', 'planttiktok', 'housetour', 'neonshadow', 'homeoffice', 'raisedby',
#            'makeitvogue', 'foodtiktok', 'valentinesday', 'yougotthis', 'stemlife']
collist=['myhobby','easydiy','christmas2020','falldiy','whereilive']
plt.rc('font', size=6)
with open('D:\\Work\\kusuri\\plts.csv','w',newline='\n',encoding='utf-8')as fin:
    writer = csv.writer(fin)
    for ht in collist:
        #ht='raisedby'
        tids = []
        ranking={}
        for file in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
            if not file.endswith('.json'):
                continue
            temp=file.split('_')

            if ht == temp[1] and hts[ht][0] == temp[2].replace('.json', ''):
                with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + file, 'r', encoding='utf-8',
                          newline='\n') as filename_input:
                    lc = 0

                    for line in filename_input:
                        z = json.loads(line)
                        if len(tids) >= 150:
                            break
                        k = ''
                        if 'id' in z.keys():
                            k = z['id']
                        elif 'itemInfos' in z.keys():
                            k = z['itemInfos']['id']
                        if k not in tids:
                            tids.append(k)
                            ranking[k]=len(tids)
                        lc += 1
        x=[]
        y=[]
        dt = hts[ht][0]
        ftr_first = np.load('D:\\awstemp\\features_data100_1\\' + ht + '\\vae_impred_' + ht + '_' + dt + '_first_0.npy')
        for item,ftr in zip(hts[ht][1:],ftr_first):
            x.append(ranking[item])
            y.append(ftr)

        Y=tsne.fit_transform(y)
        plt.scatter(Y[:,0],Y[:,1],marker='.',c='r')
        for label, i,j in zip(x,Y[:,0],Y[:,1]):
            plt.annotate(label,xy=(i,j),xytext=(0,0),textcoords='offset points')
        plt.title(ht)
        writer.writerow([ht])
        writer.writerow(x)
        writer.writerow(Y[:,0])
        writer.writerow(Y[:,1])

        #plt.show()